
package de.justphil.tcg.tcgandroidclient.gui;

import java.util.ArrayList;
import java.util.List;

import de.justphil.tcg.tcgandroidclient.R;
import de.justphil.tcg.tcgandroidclient.content.TCGContentAdapter;
import de.justphil.tcg.tcgandroidclient.content.sqlite.SqliteDeck;
import de.justphil.tcg.tcgandroidclient.content.view.ViewCard;
import de.justphil.tcg.tcgandroidclient.content.view.ViewDeck;
import de.justphil.tcg.tcgandroidclient.gui.decks.DecksAct;
import de.justphil.tcg.tcgandroidclient.network.rest.RESTUpdateService;
import android.app.Activity;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class HomeAct extends Activity {
	
	private Button playButton;
	private Button cardsButton;
	private Button settingsButton;
	private Button aboutButton;
	private Spinner decksSpinner;
	private ContentObserver coUseGetter;
	private TCGContentAdapter contentAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		contentAdapter = new TCGContentAdapter(this);
		setContentView(R.layout.home);
		initializeComponents();
	}
	
	private void initializeComponents() {
	
		playButton = (Button) findViewById(R.id.home_button_play);
		cardsButton = (Button) findViewById(R.id.home_button_cards);
		settingsButton = (Button) findViewById(R.id.home_button_settings);
		aboutButton = (Button) findViewById(R.id.home_button_about);
		decksSpinner = (Spinner) findViewById(R.id.home_spinner_decks);
		playButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				Intent intent = new Intent(HomeAct.this, PlayAct.class);
				ViewDeck deck =
						(ViewDeck) decksSpinner.getItemAtPosition(decksSpinner
								.getSelectedItemPosition());
				intent.putExtra(PlayAct.EXTRA_DECK_ID, deck.getDeckId());
				startActivity(intent);
			}
		});
		cardsButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				startActivity(new Intent(HomeAct.this, CardsAct.class));
			}
		});
	}
	
	@Override
	protected void onPause() {
	
		super.onPause();
		contentAdapter.unregisterContentObserver(getContentObserver());
	}
	
	@Override
	protected void onResume() {
	
		super.onResume();
		Intent intent = new Intent(HomeAct.this, RESTUpdateService.class);
		intent.putExtra(RESTUpdateService.EXTRA_CLEAR_CACHE, true);
		startService(intent);
		decksSpinner.setAdapter(createArrayAdapter());
	}
	
	private ArrayAdapter<ViewDeck> createArrayAdapter() {
	
		List<SqliteDeck> sqliteDecks = contentAdapter.readDecks(getContentObserver());
		List<ViewDeck> decks = new ArrayList<ViewDeck>();
		for (SqliteDeck sqliteDeck : sqliteDecks) {
			ViewDeck deck =
					new ViewDeck(contentAdapter, getContentObserver(), sqliteDeck.getId(),
							sqliteDeck.getName());
			decks.add(deck);
		}
		ArrayAdapter<ViewDeck> adapter =
				new ArrayAdapter<ViewDeck>(this, android.R.layout.simple_spinner_item,
						android.R.id.text1, decks);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return adapter;
	}
	
	private ContentObserver getContentObserver() {
	
		if (coUseGetter == null) {
			coUseGetter = new ContentObserver(new Handler()) {
				
				@Override
				public void onChange(boolean selfChange) {
				
					decksSpinner.setAdapter(createArrayAdapter());
				}
			};
		}
		return coUseGetter;
	}
}
